# -*- encoding: utf-8 -*-

# import Lib.Logger.log4py as logging
import os
import json
import time
import requests
import random


def create_subgroup_request(catalog_id, feature_id, component_id):
    return {
        "catalogueCode": 1,
        "catalogueId": catalog_id,
        "featureIds": feature_id,
        "filterType": 0,
        "marketId": 11,
        "languageId": "1",
        "componentId": component_id
    }


def create_component_request(catalog_id, feature_id):
    return {
        "catalogueCode": 1,
        "catalogueId": catalog_id,
        "featureIds": feature_id,
        "filterType": 0,
        "marketId": 11,
        "languageId": "1"
    }


def create_callout_req(catalog_id, feature_id, component_parent_id, component_id):
    return {
        "catalogueCode": 1,
        "catalogueId": catalog_id,
        "componentId": component_id,
        "parentComponentId": component_parent_id,
        "featureIds": feature_id,
        "filterType": 0,
        "languageId": "1",
        "marketId": 11,
        "vin": "",
        "shoppingListId": -1,
        "sessionData": {
            "anyCatalogueMode": True,
            "currencyCode": "GBP",
            "currencyId": 2,
            "fleetUser": False,
            "languageIsoCode": "en",
            "marketCode": "GB",
            "marketId": 11,
            "showDealershipData": False,
            "showFleet": False,
            "showFluids": False,
            "showLabours": False,
            "showParts": True,
            "showPrices": True,
            "showServices": False,
            "useStaticPrices": True
        }
    }


BRANDS = {
    "Jaguar": '1',
    "LandRover": '2'
}


class LandRoverPartFetcher:
    host = 'https://epc.jlr-apps.com'
    #host = "http://106.14.215.75"
    proxy = {
        'http': 'http://106.14.215.75:5870'
        #'https': 'http://106.14.215.75:5870'
    }
    CATALOG_LIST_URL = host + '/api/vehicles/11?vehicleTypeId={0}&languageId=24&includeFromDate=false&t={1}'
    CATALOG_URL = host + '/api/configurationSummary/catalogueDefault?catalogueId={0}&marketId=11&languageId=24&t={1}'
    SUBGROUP_URL = host + '/api/componentGroup/subGroups?catalogueCode={0}&catalogueId={1}&componentId={2}&featureIds={3}&filterType=0&languageId=24&marketId=11'
    COMPONENT_URL = host + '/api/componentGroup?catalogueCode={0}&catalogueId={1}&featureIds={2}&filterType=0&languageId=24&marketId=11'
    COMPONENT_SECTION_URL = host + '/api/componentSection?catalogueCode={0}&catalogueId={1}&componentId={2}&featureIds={3}&filterType=0&languageId=24&marketId=11'
    CALLOUT_URL = host + '/api/componentSection/callout?catalogueCode={0}&catalogueId={1}&componentId={2}&featureIds={3}&filterType=0&languageId=24&marketId=11&parentComponentId={4}&shoppingListId=-1&vin='

    def __init__(self, brand, root_dir="."):

        #self.logger = logging.get_logger('crawler', 'PartFetcher')

        self.root_dir = root_dir
        self.brand_path = os.path.join(root_dir, "LandRover")
        self.brand_id = BRANDS["LandRover"]
        self.curr_path = self.brand_path

        if not os.path.exists(self.brand_path):
            os.makedirs(self.brand_path, exist_ok=True)

    def header(self):
        header = {
                'Host': "epc.jlr-apps.com",
                'Connection': "keep-alive",
                'Accept': "application/json, text/plain, */*",
                'ClientSign': "6c2912669b37591b0e96f2bf08977efd",
                'Key': "8cd4aab2-c3d7-4b08-80c2-5d6105662ce3",
                'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                'Referer': "https://epc.jlr-apps.com/",
                'Accept-Language': "zh-CN,zh;q=0.9",
                'Accept-Encoding': "gzip, deflate, br",
                #'Cookie':"JSESSIONID=CC84234286D8A660DB60869DDB1FCECC",
                'Authorization': "Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJmdi1tb2N0OUxFenVuUzgxa2RSRmlrQnFqSVM0TVFBOUpLQ2pYMTA4WnBBIn0.eyJqdGkiOiI1YmQ5ZDA0NS1lYzBjLTQxZjgtYmUzOC1mZDNhZmU0OTA2MzMiLCJleHAiOjE1MzI2MDIwMDgsIm5iZiI6MCwiaWF0IjoxNTMyNTk0ODA4LCJpc3MiOiJodHRwczovL3ByZC5qbHItZWNhdC5jbGlmZm9yZC10aGFtZXMuY29tL2F1dGgvcmVhbG1zL2psciIsImF1ZCI6Imd1aSIsInN1YiI6IjAwMjdiNmRjLTY4M2EtNGI0Yi1iM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xU3aeRMvTvovfWEmCSfh7m2eFEYylYz7qoT5BkgIadJpVZ4tHaizZPXzuW77PKoUSjtet3z2JQ8Cx6cdm47i5Pe87jMxUbXVMFA8UvQ4gJIeVT6-KUZimC07hllHBp-A4peXQFmTniOTIjA"
                }

        heards = {
        'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
        'Key': 'f3a9b43d-77d6-4f9e-b07b-0039a41712ca',
        'ClientSign': '6c2912669b37591b0e96f2bf08977efd',
    }
        return header


    def request_get(self,url, decode_json=True):

        try:
            ret = requests.get(url,proxies=self.proxy,timeout=60)
            code = ret.status_code
            if code != 200:
                # self.logger.error(url)
                # self.logger.error(ret.text)
                print('ERROR URL: ' + url)
                print('ERROE INFO: ' + ret.text)
                return None

            if decode_json:
                try:
                    ret_json = ret.json()
                    return ret_json
                except:
                    return None

            return ret.text
        except :
            print('ConnectTimeout')
            return None


    @staticmethod
    def get_time():
        return str(int(round(time.time() * 1000)))

    @staticmethod
    def get_selected_features(primary_features):
        id_set = []
        for primary_feature in primary_features:
            for feature in primary_feature["features"]:
                if feature['selected']:
                    id_set.append(str(feature["id"]))
        return ','.join(id_set)

    def fetch_parts(self, catalog_id, feature_id, component_parent_id, component_id):
        header = self.header()
        res = {}
        section_url = LandRoverPartFetcher.COMPONENT_SECTION_URL.format(self.brand_id, catalog_id, component_id, feature_id)
        try:
            #res["section"] = self.request_get(section_url)
            res_temp = requests.request("GET",section_url,proxies = self.proxy,headers = header)
            res["section"] = res_temp.json()
            if res["section"] is None:
                #self.logger.error("Missing component section, skipping")
                print('Missing component section, skipping')
                return

            # print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))


            filename = res["section"]["componentPathDTOList"][-1]["code"]
            output_filename = os.path.join(self.curr_path, "{0}.json".format(filename))
            if os.path.exists(output_filename):
                print('File already exist, skipping -> '+ output_filename)
                return
            time.sleep(random.randint(0, 3))
            callout_url = LandRoverPartFetcher.CALLOUT_URL.format(self.brand_id, catalog_id, component_id, feature_id, component_parent_id)
            #res["parts"] = self.request_get(callout_url)
            res_temp1 = requests.request("GET",callout_url,proxies = self.proxy,headers = header)
            res["parts"] = res_temp1.json()
            if res["parts"] is None:
                # self.logger.error("Missing parts section, skipping")
                print('Missing parts section, skipping')
                return

            with open(output_filename, 'w', encoding='utf-8') as fo:
                json.dump(res, fo, ensure_ascii=True, indent=4)
                print('Saved file -> ' + output_filename)
        except KeyError as err:
            err_filename = os.path.join(self.root_dir, "{0}.txt".format('landrover_part_fetcher_err.txt'))
            with open(err_filename,'a') as f_err:
                f_err.write('ERR_PART -> '+section_url+'\n')
            print(err)


    def fetch_components(self, catalog_id, feature_id, component_id, level=0):
        header = self.header()
        subgroup_url = LandRoverPartFetcher.SUBGROUP_URL.format(self.brand_id, catalog_id, component_id, feature_id)
        #subgroup_src = self.request_get(subgroup_url)
        subgroup_src_temp01 = requests.request("GET",subgroup_url,proxies = self.proxy,headers = header)
        subgroup_src = subgroup_src_temp01.json()
        if subgroup_src is None or "subComponents" not in subgroup_src:
            # self.logger.info("No subcomponents in this subgroup")
            print('No subcomponents in this subgroup')
            return

        subgroups = subgroup_src["subComponents"]
        for subgroup in subgroups:
            filter_id = subgroup["componentTypeId"]
            subgroup_id = subgroup["componentId"]
            if filter_id == 4:
                self.fetch_parts(catalog_id, feature_id, component_id, subgroup_id)
            else:
                # self.logger.info("+++PARENT: {0:d}, SUBGROUP: {1:d}".format(component_id, subgroup_id))
                print("+++PARENT: {0:d}, SUBGROUP: {1:d}".format(component_id, subgroup_id))
                self.fetch_components(catalog_id, feature_id, subgroup_id, level + 1)

    def fetch_all(self, offset, limit):
        header = self.header()
        catalog_url = LandRoverPartFetcher.CATALOG_LIST_URL.format(self.brand_id, self.get_time())
        catalogs = self.request_get(catalog_url)
        #catalogs = requests.request("GET",catalog_url,proxies = self.proxy,headers = header)
        if not catalogs:
            return



        catalogs = catalogs[offset: offset + limit]
        for catalog in catalogs:
            catalog_id = catalog.get("componentId")
            catalog_url = LandRoverPartFetcher.CATALOG_URL.format(catalog_id, self.get_time())
            #catalog_obj = self.request_get(catalog_url)
            catalog_obj = requests.request("GET",catalog_url,proxies = self.proxy,headers = header)
            dict = catalog_obj.json()
            feature_id = self.get_selected_features(dict["primaryFeatureCodes"])

            catalog_code = dict["catalogueCode"]
            self.curr_path = os.path.join(self.brand_path, catalog_code)

            # #C02
            # catalog_code_list = ['C02','C82','C80','C78','C77','C74','C72','C69','C69','C68','C65','C54','C52','C51']
            # if catalog_code in catalog_code_list:
            #     print(catalog_code)
            #     continue

            if not os.path.exists(self.curr_path):
                os.makedirs(self.curr_path, exist_ok=True)

            out_file = os.path.join(self.curr_path, 'main.json')
            with open(out_file, 'w') as fo:
                json.dump(dict, fo, ensure_ascii=True, indent=4)

            component_url = LandRoverPartFetcher.COMPONENT_URL.format(self.brand_id, catalog_id, feature_id)
            #component_src = self.request_get(component_url)
            component_src_temp = requests.request("GET",component_url,proxies = self.proxy,headers = header)
            component_src = component_src_temp.json()
            components = component_src["subComponents"] if "subComponents" in component_src else []
            for component_parent in components:
                component_id = component_parent["componentId"]
                self.fetch_components(catalog_id, feature_id, component_id)


    def fetch_defect_page_data(self,start=0):

        defect_page_data_obj = open("E:\\LandRover\\read_pages.json",encoding='utf-8')
        d_buff = defect_page_data_obj.read()
        data_json = json.loads(d_buff)
        for i,item in enumerate(data_json["files"]):
            print("current loop times -->" + str(i))
            if start > i:
                continue
            file_name = item["file_name"]
            file_path = str(self.curr_path) + file_name[file_name.rfind("\\"):]
            if os.path.exists(file_path):
                continue
            catalog_id = item["catalog_id"]
            feature_id = item["feature_id"]
            component_id = item["component_parent_id"]
            subgroup_id = item["componentId"]
            self.fetch_parts(catalog_id, feature_id, component_id, subgroup_id)

    def fetch_images(self,start,stop):
        image_obj = open("E:\\LandRover\\langrover_image.json", encoding='utf-8')
        d_buff = image_obj.read()
        image_json = json.loads(d_buff)

        for i,item in enumerate(image_json["RECORDS"][start:stop]):

            print("current loop times -->" + str(i+start))
            image_name = item["image"]

            file_path = str(self.curr_path) + "\\image\\"+image_name+".svgz"
            if os.path.exists(file_path):
                continue
            url = LandRoverPartFetcher.IMAGE_URL.format(image_name)
            try:
                returned = requests.get(url, verify=False,timeout=60)
            except Exception:
                print("请求异常，image_name："+image_name)

            result = returned.content
            #returned = self.source.request_get_image(url,False)
            with open(file_path, 'wb') as fo:
                fo.write(returned.content)

            time.sleep(0.5)


if __name__ == '__main__':
    import sys

    offset = 0
    limit = 1000
    if len(sys.argv) > 2:
        offset = int(sys.argv[1])
        limit = int(sys.argv[2])

    fetcher = LandRoverPartFetcher(".")
    fetcher.fetch_all(offset, limit)

    # fetcher.fetch_defect_page_data(507)

    # start = 0  # index  min 0
    # stop = 10300  #  index max 10300
    # fetcher.fetch_images(start,stop);